#include "lcd-api.h"
#include "../control.h"
#include "../gpio/gpio.h"
#include "../timer/timer32.h"
#include <math.h>

//#define uint32_t unsigned int

//#define uchar unsigned char

//======= IO setting =======//
#if 0
sbit Cs	    =P3^0;
sbit Reset  =P3^1;
sbit Dc     =P3^3;
sbit Wr     =P3^4;
sbit Rd		=P3^5;
#endif

void init_LCD(void);
void Display(uint16_t c1,uint16_t x,uint16_t y);
void Trans_Com_16(uint16_t data1);
void Trans_Dat_16(uint16_t data1);
void Delayms(uint16_t i);
void Delay(uint16_t i);



//=============================//
//======LCD Display Start======//
//=============================//
#if 0
void main(void)
 {
    uchar i,temp;
  	init_LCD();
   	while(1)
    {
		Display(colorc[temp],240,320);
		Delay(20);
		i++;
                temp=i%9;
     }
 }
#endif

//=== Output LCD data port ===//
void Write_Port_LCD(uint16_t data)
{


#if 0
	    for(i=LCD_DATA_LOW_PORT_LSB;i<(LCD_DATA_LOW_PORT_MSB+1);i++)
	    {
	    	/*Write data to LCD Low Port*/
	    	int value= ((data & (1 << i))>>i);
	    	GPIOSetValue( LCD_DATA_LOW_PORT,i,value);
	    }
#endif
	    LPC_GPIO[LCD_DATA_LOW_PORT]->DATA=data;
#if 0
uint16_t i;
for(i=LCD_DATA_HIGH_PORT_LSB;i<(LCD_DATA_HIGH_PORT_MSB+1);i++)
{
	/*Write data to LCD Low Port*/
	int value= ((data & (1 << (12+i)))>>(i+12));
	GPIOSetValue( LCD_DATA_HIGH_PORT,i,value);
}
#endif

        LPC_GPIO[LCD_DATA_HIGH_PORT]->DATA = LPC_GPIO[LCD_DATA_HIGH_PORT]->DATA | (0x000F & (data>>12));
}

//=== Write reg. command ===//
void Trans_Com_16(uint16_t data1)
{
	//0=16bit; 1=8bit
	//uint16_t  high8=data1;
	//uint16_t  low8=data1;
 	//Cs=0;
	GPIOSetValue( LCD_CONTROL_PORT, LCD_CS, PIN_OFF );
 	//Dc=0;
	GPIOSetValue( LCD_CONTROL_PORT, LCD_RS, PIN_OFF );
	//Rd=1;
	GPIOSetValue( LCD_CONTROL_PORT, LCD_RD, PIN_ON );


	//Wr=1;
	GPIOSetValue( LCD_CONTROL_PORT, LCD_WR, PIN_ON );
	//P1=high8;
	//Write_Port_LCD(high8);
	Write_Port_LCD(data1);
	//Wr=0;
	GPIOSetValue( LCD_CONTROL_PORT, LCD_WR, PIN_OFF );


	//Wr=1;
	//GPIOSetValue( LCD_CONTROL_PORT, LCD_WR, PIN_ON);
	//P1=low8;
	//Write_Port_LCD(low8);
	//Wr=0;
	//GPIOSetValue( LCD_CONTROL_PORT, LCD_WR, PIN_OFF );


	//Wr=1;
	GPIOSetValue( LCD_CONTROL_PORT, LCD_WR, PIN_ON);
	//Rd=0;
	//GPIOSetValue( LCD_CONTROL_PORT, LCD_RD, PIN_OFF);
	//Dc=1;
	GPIOSetValue( LCD_CONTROL_PORT, LCD_RS, PIN_ON );
	//Cs=1;
	GPIOSetValue( LCD_CONTROL_PORT, LCD_CS, PIN_ON);

}

//=== Write reg. data ===//
void Trans_Dat_16(uint16_t data1)
{
	//uint16_t  high8= data1;
	//uint16_t  low8=data1;
	//Cs=0;
	GPIOSetValue( LCD_CONTROL_PORT, LCD_CS, PIN_OFF );
 	//Dc=1;
	//GPIOSetValue( LCD_CONTROL_PORT, LCD_RS, PIN_ON );
	//Rd=1;
	GPIOSetValue( LCD_CONTROL_PORT, LCD_RD, PIN_ON );


	//Wr=1;
	GPIOSetValue( LCD_CONTROL_PORT, LCD_WR, PIN_ON );
	//P2=high8;
	//Write_Port_LCD(high8);
	Write_Port_LCD(data1);
	//Wr=0;
	GPIOSetValue( LCD_CONTROL_PORT, LCD_WR, PIN_OFF );


	//Wr=1;
	//GPIOSetValue( LCD_CONTROL_PORT, LCD_WR, PIN_ON );
	//P1=low8;
	//Write_Port_LCD(low8);
	//Wr=0;
	//GPIOSetValue( LCD_CONTROL_PORT, LCD_WR, PIN_OFF );

	//Wr=1;
	GPIOSetValue( LCD_CONTROL_PORT, LCD_WR, PIN_ON );
	//Rd=0;
	//GPIOSetValue( LCD_CONTROL_PORT, LCD_RD, PIN_OFF );
	//Dc=0;
	//GPIOSetValue( LCD_CONTROL_PORT, LCD_RS, PIN_OFF );
	//Cs=1;
	GPIOSetValue( LCD_CONTROL_PORT, LCD_CS, PIN_ON );

}

//initial for fast transfer data
void Trans_Dat_fast_start()
{
	//Cs=0;
	GPIOSetValue( LCD_CONTROL_PORT, LCD_CS, PIN_OFF );
 	//Dc=1;
	//GPIOSetValue( LCD_CONTROL_PORT, LCD_RS, PIN_ON );
	//Rd=1;
	GPIOSetValue( LCD_CONTROL_PORT, LCD_RD, PIN_ON );
}

//reset pin for fast transfer data
void Trans_Dat_fast_end()
{
 	//Dc=0;
	//GPIOSetValue( LCD_CONTROL_PORT, LCD_RS, PIN_OFF );
	//Rd=0;
	//GPIOSetValue( LCD_CONTROL_PORT, LCD_RD, PIN_OFF );
	//Cs=1;
	GPIOSetValue( LCD_CONTROL_PORT, LCD_CS, PIN_ON );

}


//=== Write reg. data ===//
void Trans_Dat_fast_16(uint16_t data1)
{
	//uint16_t  high8= data1;
	//uint16_t  low8=data1;
#if 0
	//Cs=0;
	GPIOSetValue( LCD_CONTROL_PORT, LCD_CS, PIN_OFF );
 	//Dc=1;
	GPIOSetValue( LCD_CONTROL_PORT, LCD_RS, PIN_ON );
	//Rd=1;
	GPIOSetValue( LCD_CONTROL_PORT, LCD_RD, PIN_ON );
#endif

	//Wr=1;
	GPIOSetValue( LCD_CONTROL_PORT, LCD_WR, PIN_ON );
	//P2=high8;
	//Write_Port_LCD(high8);
	Write_Port_LCD(data1);


	//Wr=0;
	GPIOSetValue( LCD_CONTROL_PORT, LCD_WR, PIN_OFF );

	//Wr=1;
	GPIOSetValue( LCD_CONTROL_PORT, LCD_WR, PIN_ON );
	//P1=low8;
	//Write_Port_LCD(low8);
	//Wr=0;
	GPIOSetValue( LCD_CONTROL_PORT, LCD_WR, PIN_OFF );

	//Wr=1;
	GPIOSetValue( LCD_CONTROL_PORT, LCD_WR, PIN_ON );

#if 0
	//Rd=0;
	GPIOSetValue( LCD_CONTROL_PORT, LCD_RD, PIN_OFF );
	//Dc=0;
	GPIOSetValue( LCD_CONTROL_PORT, LCD_RS, PIN_OFF );
	//Cs=1;
	GPIOSetValue( LCD_CONTROL_PORT, LCD_CS, PIN_ON );
#endif
}

//=== LCD initial progress ===//

//output=1 , input=0
void set_LCD_DataIO(uint8_t inout)
{
    uint32_t i=0;
    for(i=LCD_DATA_LOW_PORT_LSB;i<(LCD_DATA_LOW_PORT_MSB+1);i++)
    {
    	/* use lcd data low port as in/output */
    	GPIOSetDir(LCD_DATA_LOW_PORT,i,inout);
    	/* no interrupt */
    	GPIOIntDisable(LCD_DATA_LOW_PORT,i);
    	GPIOSetValue(LCD_DATA_LOW_PORT,i,PIN_OFF);
    }


    for(i=LCD_DATA_HIGH_PORT_LSB;i<(LCD_DATA_HIGH_PORT_MSB+1);i++)
    {
    	/* use lcd data low port as output */
    	GPIOSetDir(LCD_DATA_HIGH_PORT,i,inout);
    	/* no interrupt */
    	GPIOIntDisable(LCD_DATA_HIGH_PORT,i);
    	GPIOSetValue(LCD_DATA_HIGH_PORT,i,PIN_OFF);
    }
}


void init_LCD(void)
{
	/*----- initial i/o pin -----*/

	/* use cs as output */
	GPIOSetDir(LCD_CONTROL_PORT,LCD_CS,OUTPUT);
	/* no interrupt */
	GPIOIntDisable(LCD_CONTROL_PORT, LCD_CS);
	/* clear cs = 1 */
	GPIOSetValue( LCD_CONTROL_PORT, LCD_CS, PIN_ON );

	/* use wr as output*/
	GPIOSetDir(LCD_CONTROL_PORT,LCD_WR,OUTPUT);
	/* no interrupt */
	GPIOIntDisable(LCD_CONTROL_PORT, LCD_WR);
	/* clear wr = 1 */
	GPIOSetValue( LCD_CONTROL_PORT, LCD_WR, PIN_ON);

	/* use rs as output */
	GPIOSetDir(LCD_CONTROL_PORT,LCD_RS,OUTPUT);
	/* no interrupt */
	GPIOIntDisable(LCD_CONTROL_PORT, LCD_RS);
	/* clear rs = 1 */
	GPIOSetValue( LCD_CONTROL_PORT, LCD_RS, PIN_ON );

	/* use rd as output */
	GPIOSetDir(LCD_CONTROL_PORT,LCD_RD,OUTPUT);
	/* no interrupt */
	GPIOIntDisable(LCD_CONTROL_PORT, LCD_RD);
	/* clear rd = 1 */
	GPIOSetValue( LCD_CONTROL_PORT, LCD_RD, PIN_ON );

	//set lcd data port io to output
	set_LCD_DataIO(OUTPUT);

#if 0
	Reset=1;
	Delayms(1);
	Reset=0;
	Delayms(5);
	Reset=1;
	Delayms(10);
#endif

	//OTM3225A-C + HYDIS 2.4"QVGA
	//VCC 2.8V
	Trans_Com_16(0x00E3); Trans_Dat_16(0x3008); // Set internal timing
	Trans_Com_16(0x00E7); Trans_Dat_16(0x0012); // Set internal timing
	Trans_Com_16(0x00EF); Trans_Dat_16(0x1231); // Set internal timing
	Trans_Com_16(0x0001); Trans_Dat_16(0x0100); // set SS and SM bit
	Trans_Com_16(0x0002); Trans_Dat_16(0x0700); // set 1 line inversion
	Trans_Com_16(0x0003); Trans_Dat_16(0x1030); // set GRAM write direction and BGR=0.
	Trans_Com_16(0x0004); Trans_Dat_16(0x0000); // Resize register
	Trans_Com_16(0x0008); Trans_Dat_16(0x0207); // set the back porch and front porch
	Trans_Com_16(0x0009); Trans_Dat_16(0x0000); // set non-display area refresh cycle ISC[3:0]
	Trans_Com_16(0x000A); Trans_Dat_16(0x0000); // FMARK function
	Trans_Com_16(0x000C); Trans_Dat_16(0x0000); // RGB interface setting
	Trans_Com_16(0x000D); Trans_Dat_16(0x0000); // Frame marker Position
	Trans_Com_16(0x000F); Trans_Dat_16(0x0000); // RGB interface polarity
	//--------------Power On sequence --------------//
	Trans_Com_16(0x0010); Trans_Dat_16(0x0000); // SAP); Trans_Dat_16(BT[3:0]); Trans_Dat_16(AP); Trans_Dat_16(DSTB); Trans_Dat_16(SLP); Trans_Dat_16(STB
	Trans_Com_16(0x0011); Trans_Dat_16(0x0007); // DC1[2:0]); Trans_Dat_16(DC0[2:0]); Trans_Dat_16(VC[2:0]
	Trans_Com_16(0x0012); Trans_Dat_16(0x0000); // VREG1OUT voltage
	Trans_Com_16(0x0013); Trans_Dat_16(0x0000); // VDV[4:0] for VCOM amplitude
	Delayms(200); // Dis-charge capacitor power voltage
	Trans_Com_16(0x0010); Trans_Dat_16(0x1490); // SAP); Trans_Dat_16(BT[3:0]); Trans_Dat_16(AP); Trans_Dat_16(DSTB); Trans_Dat_16(SLP); Trans_Dat_16(STB
	Trans_Com_16(0x0011); Trans_Dat_16(0x0227); // R11h=0x0221 at VCI=3.3V); Trans_Dat_16(DC1[2:0]); Trans_Dat_16(DC0[2:0]); Trans_Dat_16(VC[2:0]
	Delayms(50); // Delayms 50ms
	Trans_Com_16(0x0012); Trans_Dat_16(0x001c); // External reference voltage= Vci;
	Delayms(50); // Delayms 50ms
	Trans_Com_16(0x0013); Trans_Dat_16(0x0A00); // R13=0F00 when R12=009E;VDV[4:0] for VCOM amplitude
	Trans_Com_16(0x0029); Trans_Dat_16(0x000F); // R29=0019 when R12=009E;VCM[5:0] for VCOMH//0012//
	Trans_Com_16(0x002B); Trans_Dat_16(0x000D); // Frame Rate = 91Hz
	Delayms(50); // Delayms 50ms
	Trans_Com_16(0x0020); Trans_Dat_16(0x0000); // GRAM horizontal Address
	Trans_Com_16(0x0021); Trans_Dat_16(0x0000); // GRAM Vertical Address
	// ----------- Adjust the Gamma Curve ----------//
	Trans_Com_16(0x0030); Trans_Dat_16(0x0000);
	Trans_Com_16(0x0031); Trans_Dat_16(0x0203);
	Trans_Com_16(0x0032); Trans_Dat_16(0x0001);
	Trans_Com_16(0x0035); Trans_Dat_16(0x0205);
	Trans_Com_16(0x0036); Trans_Dat_16(0x030C);
	Trans_Com_16(0x0037); Trans_Dat_16(0x0607);
	Trans_Com_16(0x0038); Trans_Dat_16(0x0405);
	Trans_Com_16(0x0039); Trans_Dat_16(0x0707);
	Trans_Com_16(0x003C); Trans_Dat_16(0x0502);
	Trans_Com_16(0x003D); Trans_Dat_16(0x1008);
	//------------------ Set GRAM area ---------------//
	Trans_Com_16(0x0050); Trans_Dat_16(0x0000); // Horizontal GRAM Start Address
	Trans_Com_16(0x0051); Trans_Dat_16(MAX_WIDTH); // Horizontal GRAM End Address
	//Trans_Com_16(0x0051); Trans_Dat_16(0x000F); // Horizontal GRAM End Address
	Trans_Com_16(0x0052); Trans_Dat_16(0x0000); // Vertical GRAM Start Address
	Trans_Com_16(0x0053); Trans_Dat_16(MAX_HEIGHT); // Vertical GRAM End Address
	//Trans_Com_16(0x0053); Trans_Dat_16(0x000F); // Vertical GRAM End Address
	Trans_Com_16(0x0060); Trans_Dat_16(0xA700); // Gate Scan Line
	Trans_Com_16(0x0061); Trans_Dat_16(0x0001); // NDL,VLE); Trans_Dat_16(REV
	Trans_Com_16(0x006A); Trans_Dat_16(0x0000); // set scrolling line
	//-------------- Partial Display Control ---------//
	Trans_Com_16(0x0080); Trans_Dat_16(0x0000);
	Trans_Com_16(0x0081); Trans_Dat_16(0x0000);
	Trans_Com_16(0x0082); Trans_Dat_16(0x0000);
	Trans_Com_16(0x0083); Trans_Dat_16(0x0000);
	Trans_Com_16(0x0084); Trans_Dat_16(0x0000);
	Trans_Com_16(0x0085); Trans_Dat_16(0x0000);
	//-------------- Panel Control -------------------//
	Trans_Com_16(0x0090); Trans_Dat_16(0x0010);
	Trans_Com_16(0x0092); Trans_Dat_16(0x0600);//0x0000
	Trans_Com_16(0x0093); Trans_Dat_16(0x0003);
	Trans_Com_16(0x0095); Trans_Dat_16(0x0110);
	Trans_Com_16(0x0097); Trans_Dat_16(0x0000);
	Trans_Com_16(0x0098); Trans_Dat_16(0x0000);
	Trans_Com_16(0x0007); Trans_Dat_16(0x0133); // 262K color and display ON

	//Trans_Com_16(0x0022); //write data
	/*fill black screen*/
	//Display(0,MAX_WIDTH,MAX_HEIGHT+1);
}

//======Board Display======//
void Display(uint16_t c1,uint16_t x,uint16_t y)
 {
	uint16_t i,j;
	for(i=0;i<y;i++)
	 {
	 for(j=0;j<x;j++)
 	  {
	   Trans_Dat_16(c1);
	   }
	  }
 }

//=== Delay ms time define ===//
void Delayms(uint16_t i)
{
#if 0
	uint16_t j,k;
	for(j=0;j<i;j++)
	 for(k=0;k<200;k++);
#endif

	 delay32Ms(TIMER1,i);

}


void Delay(uint16_t i)
{
#if 0
	int j,k;
 	for(j=0;j<i;j++)
 	 for(k=0;k<8900;k++);
#endif

 	 delay32Ms(TIMER1,i*1000); //delay in second

}

//--------------- Ataya API ----------------------------

/* read pixel color from coor */
uint16_t ReadPixel(uint16_t x,uint16_t y)
{
	Trans_Com_16(0x0020); Trans_Dat_16(x); // Start x Address
	//Trans_Com_16(0x0051); Trans_Dat_16(x);
	//Trans_Com_16(0x0051); Trans_Dat_16(x+1); // Horizontal GRAM End Address
	Trans_Com_16(0x0021); Trans_Dat_16(y); // Start  y Address
	Trans_Com_16(0x0022);

	Trans_Dat_fast_start();

	//Write_Port_LCD(0);
	//set lcd data ioport as input
	set_LCD_DataIO(INPUT);

	//read dummy
	GPIOSetValue( LCD_CONTROL_PORT, LCD_RD, PIN_OFF );
	GPIOSetValue( LCD_CONTROL_PORT, LCD_RD, PIN_ON );
	GPIOSetValue( LCD_CONTROL_PORT, LCD_RD, PIN_OFF );
	GPIOSetValue( LCD_CONTROL_PORT, LCD_RD, PIN_ON );

	//read value
	GPIOSetValue( LCD_CONTROL_PORT, LCD_RD, PIN_OFF );
	GPIOSetValue( LCD_CONTROL_PORT, LCD_RD, PIN_ON );
	GPIOSetValue( LCD_CONTROL_PORT, LCD_RD, PIN_OFF );
	GPIOSetValue( LCD_CONTROL_PORT, LCD_RD, PIN_ON );

	uint32_t d1=(LPC_GPIO[LCD_DATA_LOW_PORT]->DATA);
	uint32_t d2=((LPC_GPIO[LCD_DATA_HIGH_PORT]->DATA)<<12);
	uint32_t data=0xFFFF&(d1|d2);


	Trans_Dat_fast_end();

	//set lcd data ioport back to output
	set_LCD_DataIO(OUTPUT);

	return data;
}

/* set pixel function */
void SetPixel(uint16_t color,uint16_t x,uint16_t y)
{
			Trans_Com_16(0x0020); Trans_Dat_16(x); // Start x Address
			//Trans_Com_16(0x0051); Trans_Dat_16(x);
	    	//Trans_Com_16(0x0051); Trans_Dat_16(x+1); // Horizontal GRAM End Address
	    	Trans_Com_16(0x0021); Trans_Dat_16(y); // Start  y Address
	    	//Trans_Com_16(0x0053); Trans_Dat_16(y);
	    	//Trans_Com_16(0x0053); Trans_Dat_16(y+1); // Vertical GRAM End Address
	    	//write data
	    	Trans_Com_16(0x0022);Trans_Dat_16(color);
	    	//Display(color,2,2);
}

/*set coordinate and flush just pixel color */
void SetPixel_fast(uint16_t color)
{
			//Trans_Com_16(0x0020); Trans_Dat_16(x); // Horizontal GRAM Start Address
			//Trans_Com_16(0x0051); Trans_Dat_16(x);
	    	//Trans_Com_16(0x0051); Trans_Dat_16(x+1); // Horizontal GRAM End Address
	    	//Trans_Com_16(0x0021); Trans_Dat_16(y); // Vertical GRAM Start Address
	    	//Trans_Com_16(0x0053); Trans_Dat_16(y);
	    	//Trans_Com_16(0x0053); Trans_Dat_16(y+1); // Vertical GRAM End Address
	    	//write data
	    	Trans_Dat_fast_16(color);
	    	//Display(color,2,2);
}


/*draw rectangle function*/
void DrawRectangle(uint16_t color,
				   uint16_t x,uint16_t y,
				   uint16_t width,uint16_t height,
				   uint16_t line_width)
{

	if((x>MAX_WIDTH)||(y>MAX_HEIGHT))
		return;


	int i=x,j=y; //x,y
	int dx,dy; //displacement x,y

	dy=height-line_width;

#if 0
	//draw top and bottom of rec
	for(j=y;j<(y+line_width);j++)//line width
	 for(i=x;i<(x+width);i++)
	 {
		SetPixel(color,i,j);
		SetPixel(color,i,j+dy);
	 }
#else

	for(j=y;j<(y+line_width);j++)//line width
	{	i=x;
		Trans_Com_16(0x0020); Trans_Dat_16(i); // Horizontal GRAM Start Address
		Trans_Com_16(0x0021); Trans_Dat_16(j); // Vertical GRAM Start Address
		Trans_Com_16(0x0022);

		Trans_Dat_fast_start();
		for(i=x;i<(x+width);i++)
		{
			SetPixel_fast(color);
		}
		Trans_Dat_fast_end();

		i=x;
		Trans_Com_16(0x0020); Trans_Dat_16(i); // Horizontal GRAM Start Address
		Trans_Com_16(0x0021); Trans_Dat_16(j+dy); // Vertical GRAM Start Address
		Trans_Com_16(0x0022);
		Trans_Dat_fast_start();
		for(i=x;i<(x+width);i++)
		{
			SetPixel_fast(color);
		}
		Trans_Dat_fast_end();

	}
#endif
	//draw left and right of rec
	dx=width-line_width;
#if 0
    for(j=y+line_width;j<y+height-line_width;j++)
	for(i=x;i<(x+line_width);i++)
	 {
		SetPixel(color,i,j);
		SetPixel(color,i+dx,j);
	 }
#else
    //set to flush horizontal
    Trans_Com_16(0x0003); Trans_Dat_16(0x0038);

  	for(i=x;i<(x+line_width);i++)
  	{

  		j=y+line_width;
  		Trans_Com_16(0x0020); Trans_Dat_16(i); // Horizontal GRAM Start Address
  		Trans_Com_16(0x0021); Trans_Dat_16(j); // Vertical GRAM Start Address
  		Trans_Com_16(0x0022);

  		Trans_Dat_fast_start();
  		for(j=y+line_width;j<y+height-line_width;j++)
  		{
  			SetPixel_fast(color);
  		}
  		Trans_Dat_fast_end();

  		j=y+line_width;
		Trans_Com_16(0x0020); Trans_Dat_16(i+dx); // Horizontal GRAM Start Address
		Trans_Com_16(0x0021); Trans_Dat_16(j); // Vertical GRAM Start Address
		Trans_Com_16(0x0022);

		Trans_Dat_fast_start();
		for(j=y+line_width;j<y+height-line_width;j++)
		{
			SetPixel_fast(color);
		}
		Trans_Dat_fast_end();


  	}

    //set back to write verizontal
    Trans_Com_16(0x0003); Trans_Dat_16(0x0030); // set GRAM write direction and BGR=1.
#endif

}

/*fill rectangle */
void FillRectangle(uint16_t  color,
		   uint16_t x,uint16_t y,
		   uint16_t width,uint16_t height)
{
	int i=0;
	int max=(width*height);//+height-1;

	//color=(color<<11)|(color>>11);

	//------------------ Set GRAM area ---------------//
	Trans_Com_16(0x0050); Trans_Dat_16(x); // Horizontal GRAM Start Address
	Trans_Com_16(0x0051); Trans_Dat_16(x+width-1); // Horizontal GRAM End Address
	//Trans_Com_16(0x0051); Trans_Dat_16(0x000F); // Horizontal GRAM End Address
	Trans_Com_16(0x0052); Trans_Dat_16(y); // Vertical GRAM Start Address
	Trans_Com_16(0x0053); Trans_Dat_16(y+height-1); // Vertical GRAM End Address


	Trans_Com_16(0x0020); Trans_Dat_16(x); // Start x Address
	Trans_Com_16(0x0021); Trans_Dat_16(y); // Start y Address
	Trans_Com_16(0x0022);

	Trans_Dat_fast_start();
	SetPixel_fast(color);
	for(i=0;i<(max-1);i++){

			//Wr=0;
			GPIOSetValue( LCD_CONTROL_PORT, LCD_WR, PIN_OFF );
			//Wr=1;
			GPIOSetValue( LCD_CONTROL_PORT, LCD_WR, PIN_ON );

			//Wr =0;
			GPIOSetValue( LCD_CONTROL_PORT, LCD_WR, PIN_OFF );
			//Wr=1;
			GPIOSetValue( LCD_CONTROL_PORT, LCD_WR, PIN_ON );

	}
		//SetPixel_fast(color);
	Trans_Dat_fast_end();


	//------------------ Set GRAM area back to MAX ---------------//
	Trans_Com_16(0x0050); Trans_Dat_16(0x0000); // Horizontal GRAM Start Address
	Trans_Com_16(0x0051); Trans_Dat_16(MAX_WIDTH); // Horizontal GRAM End Address
	//Trans_Com_16(0x0051); Trans_Dat_16(0x000F); // Horizontal GRAM End Address
	Trans_Com_16(0x0052); Trans_Dat_16(0x0000); // Vertical GRAM Start Address
	Trans_Com_16(0x0053); Trans_Dat_16(MAX_HEIGHT); // Vertical GRAM End Address


}


/*========= Font number 5x7 pixel=========*/

static uint32_t font_number[10][2]={
//---- 0 ------
//   01110
//   10001
//   10001
//   10001
//   10001
//   10001
//   01110
    {0x746318C5, //1 bit - 32 bit
        0x6},    //32 bit - 35 bit

//---- 1 ------
//   00100
//   01100
//   11100
//   00100
//   00100
//   00100
//   11111
    {0x23384213, //1 bit - 32 bit
        0x7 },  //32 bit - 35 bit

//---- 2 ------
//   01110
//   10001
//   00010
//   00100
//   01000
//   10000
//   11111
    {0x74444443, //1 bit - 32 bit
        0x7 },  //32 bit - 35 bit

//---- 3 ------
//   11110
//   00001
//   00001
//   11110
//   00001
//   00001
//   11110
	{0xf043e087, //1 bit - 32 bit
		0x6 },  //32 bit - 35 bit

//---- 4 ------
//   00010
//   00110
//   01010
//   10010
//   11111
//   00010
//   00010
	{0x11952F88, //1 bit - 32 bit
		0x2 },  //32 bit - 35 bit

//---- 5 ------
//   11111
//   10000
//   10000
//   01110
//   00001
//   00001
//   11110
	{0xFc20e087, //1 bit - 32 bit
		0x6 },  //32 bit - 35 bit

//---- 6 ------
//   01111
//   10000
//   10000
//   11110
//   10001
//   10001
//   01110
	{0x7c21e8c5, //1 bit - 32 bit
		0x6 },  //32 bit - 35 bit

//---- 7 ------
//   11111
//   10001
//   00001
//   00010
//   00100
//   01000
//   01000
	{0xFC422221, //1 bit - 32 bit
		0x0 },  //32 bit - 35 bit

//---- 8 ------
//   01110
//   10001
//   10001
//   01110
//   10001
//   10001
//   01110
	{0x7462e8c5, //1 bit - 32 bit
		0x6 },  //32 bit - 35 bit

//---- 9 ------
//   01110
//   10001
//   10001
//   01111
//   00001
//   00010
//   11100
	{0x7462f08b, //1 bit - 32 bit
		0x4 }  //32 bit - 35 bit

};



/*draw number 0-9
 * rotate : 0 = horizontal , 1= vertical draw*/
uint8_t DrawNumber(uint16_t color,uint16_t x,uint16_t y,uint8_t value,uint8_t backg,uint8_t rotate)
{
  if(value > 9)
    return (-1);

   uint32_t outp1=font_number[value][0];
   uint32_t outp2=font_number[value][1];


   //Trans_Com_16(0x0004); Trans_Dat_16(0x0003); // Resize register

   //set to flush vertical
   //begin on the left corner
   if(rotate==1)
      Trans_Com_16(0x0003); Trans_Dat_16(0x1018);

	//------------------ Set GRAM area ---------------//
   if(rotate==1){//flush vertical
	Trans_Com_16(0x0050); Trans_Dat_16(x); // Horizontal GRAM Start Address
	Trans_Com_16(0x0051); Trans_Dat_16(x+(FONT_HEIGHT-1));
	Trans_Com_16(0x0052); Trans_Dat_16(y); // Vertical GRAM Start Address
	Trans_Com_16(0x0053); Trans_Dat_16(y+(FONT_WIDTH-1)); // Vertical GRAM End Address

	Trans_Com_16(0x0020); Trans_Dat_16(x); // Start x Address
	Trans_Com_16(0x0021); Trans_Dat_16(y+(FONT_WIDTH-1)); // Start y Address
   }else{
	Trans_Com_16(0x0050); Trans_Dat_16(x); // Horizontal GRAM Start Address
	Trans_Com_16(0x0051); Trans_Dat_16(x+(FONT_WIDTH-1)); // Horizontal GRAM End Address
	Trans_Com_16(0x0052); Trans_Dat_16(y); // Vertical GRAM Start Address
	Trans_Com_16(0x0053); Trans_Dat_16(y+(FONT_HEIGHT-1)); // Vertical GRAM End Address

	Trans_Com_16(0x0020); Trans_Dat_16(x); // Start x Address
	Trans_Com_16(0x0021); Trans_Dat_16(y); // Start y Address
   }



	Trans_Com_16(0x0022);

	Trans_Dat_fast_start();
	//SetPixel_fast(backg);

   uint32_t i;
   //uint16_t pcolor=backg;
   for(i=0;i<32;i++){// first 32 bit
      if((outp1 & (1<<(31-i)))> 0){ //if bit==1 then draw pixel
         //SetPixel(color,x+(i%(FONT_WIDTH)),y+floor(i/(FONT_HEIGHT-2)));
    		SetPixel_fast(color);
       }else{
    		SetPixel_fast(backg);
       }
   }


   for(i=0;i<3;i++){//last 3 bit
       if((outp2 & (1<<(2-i)))>0){
          //SetPixel(color,x+(2+i),y+(FONT_HEIGHT-1));
           SetPixel_fast(color);
       }else{
		   SetPixel_fast(backg);
       }
   }

   Trans_Dat_fast_end();

   //set back to flush horizotal
   if(rotate==1)
      Trans_Com_16(0x0003); Trans_Dat_16(0x1030);

   //------------------ Set GRAM area back to MAX ---------------//
   	Trans_Com_16(0x0050); Trans_Dat_16(0x0000); // Horizontal GRAM Start Address
   	Trans_Com_16(0x0051); Trans_Dat_16(MAX_WIDTH); // Horizontal GRAM End Address
   	//Trans_Com_16(0x0051); Trans_Dat_16(0x000F); // Horizontal GRAM End Address
   	Trans_Com_16(0x0052); Trans_Dat_16(0x0000); // Vertical GRAM Start Address
   	Trans_Com_16(0x0053); Trans_Dat_16(MAX_HEIGHT); // Vertical GRAM End Address

   	//Trans_Com_16(0x0004); Trans_Dat_16(0x0000); // set back no Resize register
   	return 0;
}


/*draw image function*/
void DrawImage(uint16_t pixelcolor[],
		   uint16_t x,uint16_t y,
		   uint16_t width,uint16_t height,uint8_t rotate)
{


	   //set to flush vertical
	   //begin on the left corner
	   if(rotate==1)
	      Trans_Com_16(0x0003); Trans_Dat_16(0x1018);

		//------------------ Set GRAM area ---------------//
	   if(rotate==1){//flush vertical
		Trans_Com_16(0x0050); Trans_Dat_16(x); // Horizontal GRAM Start Address
		Trans_Com_16(0x0051); Trans_Dat_16(x+(FONT_HEIGHT-1));
		Trans_Com_16(0x0052); Trans_Dat_16(y); // Vertical GRAM Start Address
		Trans_Com_16(0x0053); Trans_Dat_16(y+(FONT_WIDTH-1)); // Vertical GRAM End Address

		Trans_Com_16(0x0020); Trans_Dat_16(x); // Start x Address
		Trans_Com_16(0x0021); Trans_Dat_16(y+(FONT_WIDTH-1)); // Start y Address
	   }else{
		Trans_Com_16(0x0050); Trans_Dat_16(x); // Horizontal GRAM Start Address
		Trans_Com_16(0x0051); Trans_Dat_16(x+(FONT_WIDTH-1)); // Horizontal GRAM End Address
		Trans_Com_16(0x0052); Trans_Dat_16(y); // Vertical GRAM Start Address
		Trans_Com_16(0x0053); Trans_Dat_16(y+(FONT_HEIGHT-1)); // Vertical GRAM End Address

		Trans_Com_16(0x0020); Trans_Dat_16(x); // Start x Address
		Trans_Com_16(0x0021); Trans_Dat_16(y); // Start y Address
	   }

		Trans_Com_16(0x0022);

	Trans_Dat_fast_start();
	uint32_t i=0;
	uint32_t max=width*height;
	for(i=0;i<(max-1);i++)
	{

		    //set pixel
		    SetPixel_fast(pixelcolor[i]);

#if 0
			//Wr=0;
			GPIOSetValue( LCD_CONTROL_PORT, LCD_WR, PIN_OFF );
			//Wr=1;
			GPIOSetValue( LCD_CONTROL_PORT, LCD_WR, PIN_ON );
			//Wr =0;
			GPIOSetValue( LCD_CONTROL_PORT, LCD_WR, PIN_OFF );
			//Wr=1;
			GPIOSetValue( LCD_CONTROL_PORT, LCD_WR, PIN_ON );
#endif
	}
		//SetPixel_fast(color);
	Trans_Dat_fast_end();

    //set back to flush horizotal
    if(rotate==1)
	  Trans_Com_16(0x0003); Trans_Dat_16(0x1030);

	//------------------ Set GRAM area back to MAX ---------------//
	Trans_Com_16(0x0050); Trans_Dat_16(0x0000); // Horizontal GRAM Start Address
	Trans_Com_16(0x0051); Trans_Dat_16(MAX_WIDTH); // Horizontal GRAM End Address
	//Trans_Com_16(0x0051); Trans_Dat_16(0x000F); // Horizontal GRAM End Address
	Trans_Com_16(0x0052); Trans_Dat_16(0x0000); // Vertical GRAM Start Address
	Trans_Com_16(0x0053); Trans_Dat_16(MAX_HEIGHT); // Vertical GRAM End Address

}

